//===========mock入参，正式脚本不包含===================
def mockTargetInfo = [
        [
                "settleGmvTarget"           : 3597410000,
                "beFxSettleGmvTarget"       : 0,
                "businessCostTarget"        : 76470000,
                "gmvTrRebateWf"             : 2500,
                "beFxGmvTrRebateWf"         : 0,
                "businessCostRebateWf"      : 200,
                "businessCostYearlyRebateWf": 3000
        ],
        [
                "settleGmvTarget"           : 3910220000,
                "beFxSettleGmvTarget"       : 0,
                "businessCostTarget"        : 83120000,
                "gmvTrRebateWf"             : 6000,
                "beFxGmvTrRebateWf"         : 0,
                "businessCostRebateWf"      : 200,
                "businessCostYearlyRebateWf": 3500
        ],
        [
                "settleGmvTarget"           : 4223040000,
                "beFxSettleGmvTarget"       : 0,
                "businessCostTarget"        : 89770000,
                "gmvTrRebateWf"             : 8250,
                "beFxGmvTrRebateWf"         : 0,
                "businessCostRebateWf"      : 200,
                "businessCostYearlyRebateWf": 4000
        ]
]
def contractInfo = [
        "id"            : 0,
        "agreementNo"   : "78001",
        "signSellerId"  : 2163218448,
        "otherSellerId" :
                [],
        "activityId"    : "2013",
        "budgetActivity":
                [
                        "TAURUS-2013-2"
                ],
        "agreementType" : "SINGLE_SELLER",
        "startTime"     : 1672502400000,
        "endTime"       : 1704038399999,
        "sendType"      : 1,
        "brandType"     : 1,
        "accountType"   : 1,
        "contractNo"    : "test2163218448_1",
        "updateTime"    : 1678706997165, targetInfo: [gearInfoList: mockTargetInfo]]
def baseStats = [
        [
                "sellerId"                                       : 2163218448,
                "settleGmvLastYear"                              : 3128175311,
                "disSettleGmvLastYear"                           : 0,
                "adJkTotalCostLastYear"                          : 61036409.8,
                "totalCostLastYear"                              : 66490170.4,
                "settleTrLastYear"                               : 0.04833044,
                "disSettleTrLastYear"                            : 0.04,
                "settleGmvYoyLastYear"                           : -0.3578541,
                "disSettleGmvYoyLastYear"                        : 0.0,
                "actyDaysBeforeLastYear"                         : 0,
                "rslrActyDaysBeforeLastYear"                     : 117,
                "adJfTotalCostLastYear"                          : 61036409.8,
                "settleGmvBeforeLastYear"                        : 1561530131,
                "disSettleGmvBeforeLastYear"                     : 0,
                "fromSellerPlatfromSettleGmvLastYear"            : 151186094,
                "disFromSellerPlatfromSettleGmvLastYear"         : 0,
                "sellerIndustryType"                             : "商家发展运营",
                "sellerIndustryGroup"                            : "珠宝玉石",
                "userName"                                       : "珠宝王大拿",
                "staffEmail"                                     : "daiyilun03",
                "saleOwner"                                      : "wangying31",
                "isXn"                                           : 0,
                "isSingle"                                       : 0,
                "isS12"                                          : 0,
                "departNameLevel1"                               : "CRM商业化",
                "departNameLevel2"                               : "服饰业务中心",
                "departNameLevel3"                               : "时尚消费组",
                "adJkTotalCostYtd"                               : 9028287.100000001,
                "adJfTotalCostYtd"                               : 9028287.100000001,
                "adJkTotalCost7d"                                : 1339438.0,
                "adJfTotalCost7d"                                : 1339438.0,
                "adJkTotalCost30d"                               : 2465602.5,
                "adJfTotalCost30d"                               : 2465602.5,
                "settleGmvYtd"                                   : 1595160972,
                "settleGmv30d"                                   : 269594474,
                "fromSellerPlatfromSettleAmtYtd"                 : 75297413,
                "fromSellerPlatfromSettleAmt30d"                 : 12772024,
                "disSettleGmvYtd"                                : 0,
                "disSettleGmv30d"                                : 0,
                "disFromSellerPlatfromSettleAmtYtd"              : 0,
                "disFromSellerPlatfromSettleAmt30d"              : 0,
                "rslrActyDaysLastYear"                           : 365,
                "actyDaysLastYear"                               : 0,
                "totalCostYtd"                                   : 23886743.6,
                "totalCost7d"                                    : 2376710.5,
                "totalCost30d"                                   : 9259290.3,
                "settleTrYtd"                                    : 0.04720365,
                "disSettleTrYtd"                                 : 0.0,
                "contractRuleId"                                 : 2163218448,
                "companyName"                                    : "佛山市南海区昱予珠宝商行",
                "ruleTemplate"                                   : "single_seller",
                "accountType"                                    : 1,
                "brandType"                                      : 1,
                "executeType"                                    : 1,
                "auditFlowId"                                    : "test2163218448",
                "status"                                         : 31,
                "l1OrgName"                                      : null,
                "l2OrgName"                                      : null,
                "startTime"                                      : 1672502400000,
                "endTime"                                        : 1704038399999,
                "creator"                                        : "sys",
                "lowSettleAmtTarget"                             : 3597410000,
                "midSettleAmtTarget"                             : 3910220000,
                "highSettleAmtTarget"                            : 4223040000,
                "lowDisSettleAmtTarget"                          : null,
                "midDisSettleAmtTarget"                          : null,
                "highDisSettleAmtTarget"                         : null,
                "lowAdCostTarget"                                : 76470000,
                "midAdCostTarget"                                : 83120000,
                "highAdCostTarget"                               : 89770000,
                "lowAdCostRebate"                                : "*30%，最高不超过商业化消耗的*2.00%",
                "midAdCostRebate"                                : "*35%���最高不超过商业化消耗的*2.00%",
                "highAdCostRebate"                               : "*40%，最高不超过商业化消耗的*2.00%",
                "lowSettleAmtRebate"                             : "*结算TR*25%",
                "midSettleAmtRebate"                             : "*结算TR*60%",
                "highSettleAmtRebate"                            : "*结算TR*82.5%",
                "lowDisSettleAmtRebate"                          : null,
                "midDisSettleAmtRebate"                          : null,
                "highDisSettleAmtRebate"                         : null,
                "securityDeposit"                                : null,
                "securityDepositStatus"                          : null,
                "frameDays"                                      : 364,
                "frameCompDays"                                  : 84,
                "isMainSlr"                                      : 1,
                "adJkTotalCostCtd"                               : 9028287.100000001,
                "adJfTotalCostCtd"                               : 9028287.100000001,
                "settleGmvCtd"                                   : 1595160972,
                "fromSellerPlatfromSettleAmtCtd"                 : 75297413,
                "disSettleGmvCtd"                                : 0,
                "disFromSellerPlatfromSettleAmtCtd"              : 0,
                "totalCostCtd"                                   : 23886743.6,
                "settleTrCtd"                                    : 0.04720365,
                "disSettleTrCtd"                                 : 0.0,
                "top1BrandPayGmvCtd"                             : 0,
                "payGmvCtd"                                      : 0,
                "isVideoSlr"                                     : 0,
                "isS12Slr"                                       : 0,
                "frameDisSettleGmvCtd"                           : 0,
                "adActyDaysLastYear"                             : 365,
                "frameDisFromSellerPlatfromSettleAmtCtd"         : 0,
                "frameDisSettleTrCtd"                            : 0.0,
                "settleGmvLastMonthCtd"                          : 1417896040,
                "fromSellerPlatfromLastMonthCtd"                 : 66780880,
                "disSettleGmvLastMonthCtd"                       : 0,
                "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 0,
                "frameDisSettleGmvLastMonthCtd"                  : 0,
                "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 0,
                "settleTrLastMonthCtd"                           : 0.04709857,
                "disSettleTrLastMonthCtd"                        : 0.0,
                "frameDisSettleTrLastMonthCtd"                   : 0.0,
                "isContractRisk"                                 : 0,
                "pDate"                                          : "20230326"
        ]
]
//品牌信息
//def brandStats = [
//        [
//                "pDate"           : "20230326",
//                "sellerId"        : 2931992237,
//                "ksBrandId"       : -1,
//                "outerBrandLevel" : null,
//                "selfSettleGmvCtd": 103609019,
//                "contractRuleId"  : 2931992237,
//                "ksBrandName"     : null
//        ],
//        [
//                "pDate"           : "20230326",
//                "sellerId"        : 2931992237,
//                "ksBrandId"       : 95038,
//                "outerBrandLevel" : "其他",
//                "selfSettleGmvCtd": 990,
//                "contractRuleId"  : 2931992237,
//                "ksBrandName"     : "其他"
//        ]
//]
def brandStats = []
//商业化消耗
//def businessStats = [
//        [
//                "pDate"             : "20230326",
//                "mainSellerId"      : 2931992237,
//                "contractRuleId"    : 2931992237,
//                "isShortVideoSeller": 0
//        ]
//]
def businessStats = []
//当前季度
def quarter = 1
//当前结算周期结束时间
def settlementEnd = '2023-03-26 00:00:00'


import cn.hutool.core.collection.CollUtil
import cn.hutool.core.date.DateUtil
import cn.hutool.core.util.NumberUtil

import java.math.RoundingMode


if (contractInfo == null) {
    throw new IllegalArgumentException("contractInfo is null")
}
//===========合同基本信息======================
//框类型
def agreementType = contractInfo.agreementType
//是否品牌
def isBrandSeller = contractInfo.brandType == 1
//是否是合框
def combineAgreementType = agreementType in ['COMBINE_CUSTOM_SELLER', 'COMBINE_CUSTOM_SELLER_GOODS', 'COMBINE_NORMAL_SELLER', 'COMBINE_NORMAL_SELLER_GOODS']
//是否单边框
def singleAgreementType = agreementType in ['SINGLE_SELLER', 'SINGLE_SELLER_GOODS']
//是否主播框
def sellerAgreementType = agreementType in ['SINGLE_SELLER', 'COMBINE_CUSTOM_SELLER', 'COMBINE_NORMAL_SELLER']
//是否主播+货主框
def sellerGoodsAgreementType = agreementType in ['SINGLE_SELLER_GOODS', 'COMBINE_CUSTOM_SELLER_GOODS', 'COMBINE_NORMAL_SELLER_GOODS']
def Q = quarter as int
//计费结果
def result = [settlementGmvRebate: 0, disSettlementGmvRebate: 0, businessRebate: 0]
def attach = [:]
result.attach = attach
result.ecBill = true
result.ecBillRebate = 0
result.businessBill = combineAgreementType
result.businessBillRebate = 0
attach << [settlementFlag: true]
if (CollUtil.isEmpty(baseStats)) {
    attach << [settlementFlag: false, unSettlementCode: 'baseStatsEmpty']
    return result
}

//=============合框 & 品牌商家需要判断：  所有店铺的 top1品牌GMV ÷ 所有店铺总GMV ≥ 60%
//=============合框 & 短视频商家 & 非S12品牌商家:  商业化返点 = max (1.5%，实际档位对应的返点比例-1.5%）
//是否短视频商家
def shortVideoSeller = CollUtil.isNotEmpty(businessStats) && businessStats[0].isShortVideoSeller == 1
def s12BrandSeller = false
//商业化返点特殊处理
if (isBrandSeller && combineAgreementType) {
    def top1Brand
    def totalBrandGmv = 0
    if (CollUtil.isNotEmpty(brandStats)) {
        brandStats.findAll { it.ksBrandId == null }.each { it.ksBrandId = -1 }
        def groupBrand = brandStats.groupBy { it.ksBrandId }
                .collect { k, v -> [ksBrandId: k, outerBrandLevel: v.get(0).outerBrandLevel, selfSettleGmvCtd: v.sum { it.selfSettleGmvCtd }] }
                .sort { it.selfSettleGmvCtd }.reverse()
        top1Brand = groupBrand.find { it.ksBrandId > 0 }
        totalBrandGmv = brandStats.sum { it.selfSettleGmvCtd }
    }
    //是否S12Seller商家
    s12BrandSeller = top1Brand != null && top1Brand.outerBrandLevel in ['S1', 'S2']
    //top1品牌gmv
    def top1BrandGmv = top1Brand != null ? top1Brand.selfSettleGmvCtd : 0
    //top1品牌gmv占比
    def top1BrandGmvRate = totalBrandGmv == 0 ? 0d : NumberUtil.div(top1BrandGmv, totalBrandGmv, 4, RoundingMode.HALF_UP)
    attach << [top1BrandGmv: top1BrandGmv, totalBrandGmv: totalBrandGmv, top1BrandGmvRate: NumberUtil.toStr(top1BrandGmvRate), shortVideoSeller: shortVideoSeller, s12BrandSeller: s12BrandSeller]
    if (top1BrandGmvRate < 0.6d) {
        attach << [settlementFlag: false, unSettlementCode: 'brandGmvNotReach']
        result.attach = attach
        return result
    }
}

//=============年框基础指标计算========================================
//结算GMV 合同生效至今累计值
long settlementGmv = baseStats.sum { it.settleGmvCtd }
//分销结算GMV 合同生效至今累计值
long disSettlementGmv = sellerGoodsAgreementType ? baseStats.sum { it.frameDisSettleGmvCtd } : baseStats.sum { it.disSettleGmvCtd }
//商业化计框口径 合同生效至今累计值
long jkBusinessTotalCost = NumberUtil.round(baseStats.sum { it.adJkTotalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//商业化计返口径 合同生效至今累计值
long jfBusinessTotalCost = NumberUtil.round(baseStats.sum { it.adJfTotalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//电商口径商业化消耗 合同生效至今累计值
long dsBusinessTotalCost = NumberUtil.round(baseStats.sum { it.totalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//结算TR
def divF = { long a, long b ->
    if (b == 0L) {
        return 0d
    }
    return NumberUtil.div(a, b, 4, RoundingMode.HALF_UP).doubleValue()
}
def settleTrLastMonth = {
    if (Q in [1, 2, 3]) {
        return divF(baseStats.sum { it.fromSellerPlatfromLastMonthCtd }, baseStats.sum { it.settleGmvLastMonthCtd })
    } else if (Q == 4) {
        return divF(baseStats.sum { it.fromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.settleGmvCtd })
    }
}
//分销结算TR
def disSettleTrLastMonth = {
    if (Q in [1, 2, 3]) {
        //主播+货主框
        if (sellerGoodsAgreementType) {
            return divF(baseStats.sum { it.frameDisFromSellerPlatfromSettleAmtLastMonthCtd }, baseStats.sum { it.frameDisSettleGmvLastMonthCtd })
        } else {
            return divF(baseStats.sum { it.disFromSellerPlatfromSettleAmtLastMonthCtd }, baseStats.sum { it.disSettleGmvLastMonthCtd })
        }
    } else if (Q == 4) {
        //主播+货主框
        if (sellerGoodsAgreementType) {
            return divF(baseStats.sum { it.frameDisFromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.frameDisSettleGmvCtd })
        } else {
            return divF(baseStats.sum { it.disFromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.disSettleGmvCtd })
        }
    }
}

//目标完成档位
def targetInfo = contractInfo.targetInfo.gearInfoList
def sendType = contractInfo.sendType
//框架总天数=合同结束日期-合同生效日期+1
def agreementTotalDays = DateUtil.betweenDay(DateUtil.date(contractInfo.startTime), DateUtil.date(contractInfo.endTime), true) + 1
//已执行天数=结算结束日期-合同开始时间
def agreementPerformDays = DateUtil.betweenDay(DateUtil.date(contractInfo.startTime), DateUtil.parse(settlementEnd), true) + 1
def completionTarget = targetInfo.get(sendType - 1)
//目标完成度
def quarterRateMap = [1: 0.86d, 2: 0.883d, 3: 0.905d, 4: 1d]
//累计时间进度 累计框架已执行天数/框架总天数
def agreementPerformProcess = NumberUtil.round(agreementPerformDays / agreementTotalDays, 4, RoundingMode.HALF_UP).doubleValue()
//完成率要求 = 累计时间进度 * 对应季度百分比
def completionRateTarget = NumberUtil.round(agreementPerformProcess * (quarterRateMap.get(Q)), 4, RoundingMode.HALF_UP).doubleValue()
attach += [settlementGmv       : settlementGmv, disSettlementGmv: disSettlementGmv, jfBusinessTotalCost: jfBusinessTotalCost,
           jkBusinessTotalCost : jkBusinessTotalCost, dsBusinessTotalCost: dsBusinessTotalCost, agreementTotalDays: agreementTotalDays,
           agreementPerformDays: agreementPerformDays, agreementPerformProcess: NumberUtil.toStr(agreementPerformProcess), completionRateTarget: NumberUtil.toStr(completionRateTarget)]

//=============Q1~ Q3 当目标完成度>=完成率要求时，进行奖励结算=========
if (Q in [1, 2, 3]) {
    //计算GMV的完成度
    def settlementGmvCompletionRate = divF(settlementGmv, completionTarget.settleGmvTarget)
    def businessTotalCost = combineAgreementType ? jkBusinessTotalCost : dsBusinessTotalCost
    def businessTotalCostCompletionRate = divF(businessTotalCost, completionTarget.businessCostTarget)
    def completionRate
    if (sellerAgreementType) {
        completionRate = NumberUtil.min(settlementGmvCompletionRate, businessTotalCostCompletionRate)
    } else if (sellerGoodsAgreementType) {
        def disSettlementGmvCompletionRate = divF(disSettlementGmv, completionTarget.beFxSettleGmvTarget)
        attach << [disSettlementGmvCompletionRate: NumberUtil.toStr(disSettlementGmvCompletionRate)]
        completionRate = NumberUtil.min(settlementGmvCompletionRate, disSettlementGmvCompletionRate, businessTotalCostCompletionRate)
    }
    attach << [settlementGmvCompletionRate: NumberUtil.toStr(settlementGmvCompletionRate), businessTotalCostCompletionRate: NumberUtil.toStr(businessTotalCostCompletionRate), completionRate: NumberUtil.toStr(completionRate)]
    //不满足完成率，不结算
    if (completionRate <= completionRateTarget) {
        attach << [settlementFlag: false, unSettlementCode: 'completionRateNotReach']
        result.attach = attach
        return result
    }
}

//====================Q4，需要按照实际档位进行结算================================
//最终结算档位
def rebateLevel = sendType
if (Q == 4) {
    def targetLevel = { value, Closure cls ->
        if (value < cls(targetInfo.get(0))) {
            return -1
        } else if (value >= cls(targetInfo.get(0)) && value < cls(targetInfo.get(1))) {
            return 1
        } else if (value >= cls(targetInfo.get(1)) && value < cls(targetInfo.get(2))) {
            return 2
        } else {
            return 3
        }
    }
    def settlementGmvLevel = targetLevel(settlementGmv, { it.settleGmvTarget })
    def businessTotalCost = combineAgreementType ? jkBusinessTotalCost : dsBusinessTotalCost
    def businessGmvLevel = targetLevel(businessTotalCost, { it.businessCostTarget })
    if (sellerAgreementType) {
        rebateLevel = NumberUtil.min(settlementGmvLevel, businessGmvLevel)
    } else {
        def disSettlementGmvLevel = targetLevel(disSettlementGmv, { it.beFxSettleGmvTarget })
        rebateLevel = NumberUtil.min(settlementGmvLevel, disSettlementGmvLevel, businessGmvLevel)
    }
    //完成率不达标
    if (rebateLevel == -1) {
        attach << [settlementFlag: false, unSettlementCode: 'completionRateNotReach']
        result.attach = attach
        return result
    }
}


//=========================进行最终的返点金额计算==================================
//==============================================================================
def target = targetInfo.get(rebateLevel - 1)
attach += [rebateLevel: rebateLevel]
//========结算GMV返点==================================
//合框 && 低目标没有结算GMV返点
if (rebateLevel > 1 || singleAgreementType) {
    //增量结算GMV
    long incSettlementGmv = baseStats.sum {
        //22年运营天数
        def settlementDaysLastYear = Math.min(365, it.rslrActyDaysLastYear as long + 65)
        //22年日均结算GMV
        def settlementGmvAvgDaily = settlementDaysLastYear == 0 ? 0L : NumberUtil.div(it.settleGmvLastYear, settlementDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量结算GMV = 实际结算GMV-22年日均结算GMV * 年框已执行天数
        return it.settleGmvCtd - settlementGmvAvgDaily * agreementPerformDays
    }
    incSettlementGmv = incSettlementGmv > 0 ? incSettlementGmv : 0L
    def settlementTr = settleTrLastMonth()
    if (settlementTr < 0.006d) {
        result.settlementGmvRebate = 0
    } else {
        result.settlementGmvRebate = NumberUtil.round(incSettlementGmv * (settlementTr - 0.006d) * (target.gmvTrRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    }
    attach << [incSettlementGmv: incSettlementGmv, settlementTr: NumberUtil.toStr(settlementTr)]
}

//========分销结算GMV返点==================================
//只有主播+货主框，才会有分销结算GMV
if (sellerGoodsAgreementType) {
    long incDisSettlementGmv = baseStats.sum {
        //如果存在风控关联关系，则被被分销gmv一律置0
        if (it.isContractRisk == 1) {
            return 0L
        }
        //22年日均分销结算GMV
        def disSettlementGmvAvgDaily = it.actyDaysLastYear == 0 ? 0L : NumberUtil.div(it.disSettleGmvLastYear, it.actyDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量被分销结算GMV = 实际分销结算GMV-22年日均分销结算GMV * 年框已执行天数
        return (sellerGoodsAgreementType ? it.frameDisSettleGmvCtd : it.disSettleGmvCtd) - disSettlementGmvAvgDaily * agreementPerformDays
    }
    incDisSettlementGmv = incDisSettlementGmv > 0L ? incDisSettlementGmv : 0L
    def disSettlementTr = disSettleTrLastMonth()
    if (disSettlementTr < 0.006d) {
        result.disSettlementGmvRebate = 0L
    } else {
        result.disSettlementGmvRebate = NumberUtil.round(incDisSettlementGmv * (disSettlementTr - 0.006d) * (target.beFxGmvTrRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    }
    attach << [incDisSettlementGmv: incDisSettlementGmv, disSettlementTr: NumberUtil.toStr(disSettlementTr)]
}

//========商业化消耗GMV返点==================================
//合框 商业化消耗返点金额 = 实际商业化消耗 * 返点比例
if (combineAgreementType) {
    def costRebate = NumberUtil.round(target.businessCostRebateWf / 10000, 4, RoundingMode.HALF_UP).doubleValue()
    //短视频商家 & 非S12品牌商家，需要商业化返点特殊处理
    if (shortVideoSeller && !s12BrandSeller) {
        costRebate = NumberUtil.round(NumberUtil.max(0.015d, costRebate - 0.015d), 4, RoundingMode.HALF_UP).doubleValue()
    }
    result.businessRebate = NumberUtil.round(jfBusinessTotalCost * costRebate, 0, RoundingMode.HALF_UP).longValue()
    //电商单边框 商业化消耗返点金额 = 增量商业化消耗 * 返点比例
} else if (singleAgreementType) {
    long incBusinessTotalCost = baseStats.sum {
        //22年日均商业化消耗
        def businessTotalCostAvgDaily = it.adActyDaysLastYear == 0 ? 0L : NumberUtil.div(it.totalCostLastYear, it.adActyDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量商业化消耗
        return it.totalCostCtd - businessTotalCostAvgDaily * agreementPerformDays
    }
    incBusinessTotalCost = incBusinessTotalCost > 0L ? incBusinessTotalCost : 0L
    def businessRebate = NumberUtil.round(incBusinessTotalCost * (target.businessCostYearlyRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    //商业化消耗中 = min (增量消耗*xx%，23年全量消耗*返点比例）
    result.businessRebate = Math.min(businessRebate, NumberUtil.round(dsBusinessTotalCost * (target.businessCostRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue())
    attach << [incBusinessTotalCost: incBusinessTotalCost]
}
result.totalRebate = result.settlementGmvRebate + result.disSettlementGmvRebate + result.businessRebate
if (combineAgreementType) {
    result.ecBillRebate = result.settlementGmvRebate + result.disSettlementGmvRebate
} else {
    result.ecBillRebate = result.settlementGmvRebate + result.disSettlementGmvRebate + result.businessRebate
}
if (result.businessBill) {
    result.businessBillRebate = result.businessRebate
}
result.attach = attach
return result